package com.gzm.springcloud.controller;

import com.gzm.springcloud.pojo.Dept;
import com.gzm.springcloud.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName deptController
 * @Description:
 * @Author gzm
 * @Date 2020/11/23 10:50
 **/
@Api(description = "部门接口")
@RestController
@RequestMapping("/dept")
public class deptController {
    @Autowired
    private DeptService service;

    @Autowired
    private DiscoveryClient client;

    @ApiOperation("添加")
    @PostMapping(value="add")
    public boolean add(@RequestBody Dept dept)
    {
        return service.add(dept);
    }

    @ApiOperation("根据id查询")
    @GetMapping(value="/get/{id}")
    public Dept get(@PathVariable("id") Long id)
    {
        return service.get(id);
    }

    @ApiOperation("查询所有部门数据")
    @GetMapping(value="/list")
    public List<Dept> list()
    {
        return service.list();
    }
    @GetMapping(value = "/discovery")
    public Object discovery()
    {
        List<String> list = client.getServices();
        System.out.println("**********" + list);

        List<ServiceInstance> srvList = client.getInstances("woolcloud-dept");
        for (ServiceInstance element : srvList) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
                    + element.getUri());
        }
        return this.client;
    }

}
